﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using FineAdmin.Common;
using FineAdmin.IService;
using FineAdmin.Model;
using FineAdmin.Web.Controllers;

namespace FineAdmin.Web.Areas.File.Controllers
{
    public class FileSetController : BaseController
    {

        // GET: File/FileSet
        public override ActionResult Index(int? id)
        {
            base.Index(id);
            return View();
        }

        public IUpFileService UpFileService { get; set; }

        [HttpGet]
        public JsonResult List(UpFileModel model, PageInfo pageInfo)
        {
            var result = UpFileService.GetListByFilter(model, pageInfo);
            return Json(result, JsonRequestBehavior.AllowGet);
        }

        public ActionResult Add()
        {
            ViewBag.UploadFileSize = Configs.GetValue("UploadFileSize");
            ViewBag.UploadFileType = Configs.GetValue("UploadFileType");
            return View();
        }
        [HttpPost]
        public ActionResult Add(UpFileModel model)
        {
            //model.UserPassword = Md5.md5(Configs.GetValue("InitUserPwd"), 32);
            model.CreateTime = DateTime.Now;
            model.CreateUserId = Operator.UserId;
            model.UpdateTime = DateTime.Now;
            model.UpdateUserId = Operator.UserId;
            var result = UpFileService.Insert(model) ? SuccessTip("添加成功") : ErrorTip("添加失败");
            return Json(result);
        }

        [HttpGet]
        public JsonResult Delete(int id)
        {
            var fileData= UpFileService.GetById(id);
            string FilePath = Server.MapPath(fileData.FilePath);//转换物理路径
            if (System.IO.File.Exists(FilePath))//判断文件是否存在bai
            {
                System.IO.File.Delete(FilePath);//执行IO文件删du除,需引入命名空间zhiSystem.IO;
            }

            var result = UpFileService.DeleteById(id) ? SuccessTip("删除成功") : ErrorTip("删除失败");

            return Json(result, JsonRequestBehavior.AllowGet);
        }


        public JsonResult ExportFile()
        {
            UploadFile uploadFile = new UploadFile();
            try
            {
                var file = Request.Files[0];    //获取选中文件
                var filecombin = file.FileName.Split('.');
                if (file == null || string.IsNullOrEmpty(file.FileName) || file.ContentLength == 0 || filecombin.Length < 2)
                {
                    uploadFile.code = -1;
                    uploadFile.src = "";
                    uploadFile.msg = "上传出错!请检查文件名或文件内容";
                    return Json(uploadFile, JsonRequestBehavior.AllowGet);
                }
                //定义本地路径位置
                string localPath = Server.MapPath("~/UpFile");
                string filePathName = string.Empty; //最终文件名
                filePathName = Common.Common.CreateNo() + "." + filecombin[1];
                //Upload不存在则创建文件夹
                if (!System.IO.Directory.Exists(localPath))
                {
                    System.IO.Directory.CreateDirectory(localPath);
                }
                file.SaveAs(Path.Combine(localPath, filePathName));  //保存图片
                uploadFile.code = 0;
                uploadFile.src = Path.Combine("/UpFile/", filePathName);
                uploadFile.msg = "上传成功";

                //写入数据库
                UpFileService.Insert(new UpFileModel()
                {
                    CreateTime = DateTime.Now,
                    CreateUserId = Operator.UserId,
                    UpdateTime = DateTime.Now,
                    UpdateUserId = Operator.UserId,
                    FileName = file.FileName,
                    FileNewName = filePathName,
                    FilePath = uploadFile.src,
                    FileSize = file.ContentLength.ToString(),
                    FileType = file.ContentType
                });
                return Json(uploadFile, JsonRequestBehavior.AllowGet);
            }
            catch (Exception)
            {
                uploadFile.code = -1;
                uploadFile.src = "";
                uploadFile.msg = "上传出错!程序异常";
                return Json(uploadFile, JsonRequestBehavior.AllowGet);
            }
        }
    }
}